{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Connecting To SF Session"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from snowflake.snowpark import Session\n",
    "import snowflake.snowpark.types as T\n",
    "import pandas as pd\n",
    "import configparser\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# Loading Credentials From Config File\n",
    "snowflake_credentials_file = '../snowflake_creds.config'\n",
    "config = configparser.ConfigParser()\n",
    "config.read(snowflake_credentials_file)\n",
    "connection_parameters = dict(config['default'])\n",
    "\n",
    "session = Session.builder.configs(connection_parameters).create()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Snowflake Object Creations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "session.sql(\"CREATE WAREHOUSE IF NOT EXISTS COMPUTE_WH WITH WAREHOUSE_SIZE='X-SMALL'\").collect()\n",
    "session.sql(\"CREATE DATABASE IF NOT EXISTS SNOWPARK_DEFINITIVE_GUIDE\").collect()\n",
    "session.sql(\"CREATE SCHEMA IF NOT EXISTS SNOWPARK_DEFINITIVE_GUIDE.MY_SCHEMA\").collect()\n",
    "session.sql(\"CREATE STAGE IF NOT EXISTS SNOWPARK_DEFINITIVE_GUIDE.MY_SCHEMA.MY_STAGE\").collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Schema Definition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "bsd_schema = T.StructType([\n",
    "        T.StructField('DATETIME', T.TimestampType()),T.StructField('SEASON', T.IntegerType()),\n",
    "        T.StructField('HOLIDAY', T.IntegerType()),T.StructField('WORKINGDAY', T.IntegerType()),\n",
    "        T.StructField('WEATHER', T.IntegerType()),T.StructField('TEMP', T.FloatType()),\n",
    "        T.StructField('ATEMP', T.FloatType()),T.StructField('HUMIDITY', T.IntegerType()),\n",
    "        T.StructField('WINDSPEED', T.FloatType()),T.StructField('CASUAL', T.IntegerType()),\n",
    "        T.StructField('REGISTERED', T.IntegerType()),T.StructField('COUNT', T.IntegerType())\n",
    "])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Read & Transform CSV To Load"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>DATETIME</th>\n",
       "      <th>SEASON</th>\n",
       "      <th>HOLIDAY</th>\n",
       "      <th>WORKINGDAY</th>\n",
       "      <th>WEATHER</th>\n",
       "      <th>TEMP</th>\n",
       "      <th>ATEMP</th>\n",
       "      <th>HUMIDITY</th>\n",
       "      <th>WINDSPEED</th>\n",
       "      <th>CASUAL</th>\n",
       "      <th>REGISTERED</th>\n",
       "      <th>COUNT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2011-01-01 00:00:00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>9.84</td>\n",
       "      <td>14.395</td>\n",
       "      <td>81</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2011-01-01 01:00:00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>9.02</td>\n",
       "      <td>13.635</td>\n",
       "      <td>80</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8</td>\n",
       "      <td>32</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2011-01-01 02:00:00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>9.02</td>\n",
       "      <td>13.635</td>\n",
       "      <td>80</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5</td>\n",
       "      <td>27</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2011-01-01 03:00:00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>9.84</td>\n",
       "      <td>14.395</td>\n",
       "      <td>75</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>10</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2011-01-01 04:00:00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>9.84</td>\n",
       "      <td>14.395</td>\n",
       "      <td>75</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             DATETIME  SEASON  HOLIDAY  WORKINGDAY  WEATHER  TEMP   ATEMP  \\\n",
       "0 2011-01-01 00:00:00       1        0           0        1  9.84  14.395   \n",
       "1 2011-01-01 01:00:00       1        0           0        1  9.02  13.635   \n",
       "2 2011-01-01 02:00:00       1        0           0        1  9.02  13.635   \n",
       "3 2011-01-01 03:00:00       1        0           0        1  9.84  14.395   \n",
       "4 2011-01-01 04:00:00       1        0           0        1  9.84  14.395   \n",
       "\n",
       "   HUMIDITY  WINDSPEED  CASUAL  REGISTERED  COUNT  \n",
       "0        81        0.0       3          13     16  \n",
       "1        80        0.0       8          32     40  \n",
       "2        80        0.0       5          27     32  \n",
       "3        75        0.0       3          10     13  \n",
       "4        75        0.0       0           1      1  "
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bsd_train = pd.read_csv(\"../datasets/bike_sharing_demand.csv\")\n",
    "bsd_train.columns = ['DATETIME','SEASON','HOLIDAY','WORKINGDAY','WEATHER','TEMP','ATEMP','HUMIDITY','WINDSPEED','CASUAL','REGISTERED','COUNT']\n",
    "table_name = \"BSD_TRAIN\"\n",
    "bsd_train['DATETIME'] = pd.to_datetime(bsd_train['DATETIME'])\n",
    "bsd_train.head()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Writing It As Snowflake Table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----------------------------------------------------------------------------------------------------------------------------------------------------------\n",
      "|\"DATETIME\"           |\"SEASON\"  |\"HOLIDAY\"  |\"WORKINGDAY\"  |\"WEATHER\"  |\"TEMP\"  |\"ATEMP\"  |\"HUMIDITY\"  |\"WINDSPEED\"  |\"CASUAL\"  |\"REGISTERED\"  |\"COUNT\"  |\n",
      "-----------------------------------------------------------------------------------------------------------------------------------------------------------\n",
      "|2011-01-01 00:00:00  |1         |0          |0             |1          |9.84    |14.395   |81          |0.0          |3         |13            |16       |\n",
      "|2011-01-01 01:00:00  |1         |0          |0             |1          |9.02    |13.635   |80          |0.0          |8         |32            |40       |\n",
      "|2011-01-01 02:00:00  |1         |0          |0             |1          |9.02    |13.635   |80          |0.0          |5         |27            |32       |\n",
      "|2011-01-01 03:00:00  |1         |0          |0             |1          |9.84    |14.395   |75          |0.0          |3         |10            |13       |\n",
      "|2011-01-01 04:00:00  |1         |0          |0             |1          |9.84    |14.395   |75          |0.0          |0         |1             |1        |\n",
      "|2011-01-01 05:00:00  |1         |0          |0             |2          |9.84    |12.88    |75          |6.0032       |0         |1             |1        |\n",
      "|2011-01-01 06:00:00  |1         |0          |0             |1          |9.02    |13.635   |80          |0.0          |2         |0             |2        |\n",
      "|2011-01-01 07:00:00  |1         |0          |0             |1          |8.2     |12.88    |86          |0.0          |1         |2             |3        |\n",
      "|2011-01-01 08:00:00  |1         |0          |0             |1          |9.84    |14.395   |75          |0.0          |1         |7             |8        |\n",
      "|2011-01-01 09:00:00  |1         |0          |0             |1          |13.12   |17.425   |76          |0.0          |8         |6             |14       |\n",
      "-----------------------------------------------------------------------------------------------------------------------------------------------------------\n",
      "\n"
     ]
    }
   ],
   "source": [
    "sf_df = session.create_dataframe(bsd_train, schema=bsd_schema)\n",
    "sf_df.write.mode(\"overwrite\").save_as_table(table_name)\n",
    "sf_df.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "def_gui_3.8_env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
